package com.ilink.groundservice.controller;/**
 * <h3>iLink-java-backend-zhangbo2</h3>
 *
 * @Description <p>数据广场与前端的交互接口</p>
 * @Author ZhangBo
 * @Date 2024-09-15 21:03
 **/

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ilink.groundservice.entity.PO.dataset.DatasetIntroduction;
import com.ilink.groundservice.entity.PO.dataset.DatasetPublish;
import com.ilink.groundservice.entity.VO.dataset.DatasetResponse;
import com.ilink.groundservice.service.dataset.DatasetIntroductionService;
import com.ilink.groundservice.service.dataset.DatasetMetadataService;
import com.ilink.groundservice.service.dataset.DatasetPublishService;
import com.ilink.groundservice.service.dataset.DatasetTagsService;
import com.ilink.ilinkapi.dto.ground.dataset.DatasetMetadataRequest;
import com.ilink.ilinkcommon.domain.R;
import java.util.List;

import com.ilink.ilinkcommon.log.method.MethodLog;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DatasetController
 * @Description 数据广场与前端的交互接口
 * @Author ZhangBo
 * @Date 2024-09-15 21:03
 **/
@RestController
@RequestMapping("/ground/dataset")
@RequiredArgsConstructor
@Slf4j
public class DatasetController {

    private final DatasetMetadataService datasetMetadataService;
    private final DatasetTagsService datasetTagsService;
    private final DatasetIntroductionService datasetIntroductionService;
    private final DatasetPublishService datasetPublishService;

    /**
     * @Author ZhangBo
     * @Description 分页查询数据广场元数据
     * @Date 22:23 2024/9/15
     * @Param [request]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "分页查询数据广场元数据")
    @PostMapping("/get_public_dataset_metadata")
    public R getPublicDatasetMetadata(@RequestBody DatasetMetadataRequest request) {
        try {
            IPage<DatasetResponse> datasetResponsePage = datasetMetadataService.getPublicDatasetMetadata(request);
            if (datasetResponsePage != null) {
                return R.ok("分页查询数据广场元数据", datasetResponsePage);
            } else {
                return R.error("未查询到数据广场元数据");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 增加数据集的download_num, 并且返回下载链接
     * @Date 15:56 2024/9/16
     * @Param [jsonString]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "增加数据集的下载次数并返回下载链接")
    @PostMapping("/download_dataset")
    public R downloadDataset(@RequestBody String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String targetId = jsonObject.getString("targetId");

            String url = datasetMetadataService.downloadDataset(targetId);
            if (url != null) {
                return R.ok("返回数据集下载链接", url);
            } else {
                return R.error("未查询到数据集下载链接");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 增加数据集的share_num, 并且返回下载链接
     * @Date 15:56 2024/9/16
     * @Param [jsonString]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "增加数据集的分享次数并返回下载链接")
    @PostMapping("/share_dataset")
    public R shareDataset(@RequestBody String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String targetId = jsonObject.getString("targetId");

            String url = datasetMetadataService.shareDataset(targetId);
            if (url != null) {
                return R.ok("返回数据集分享链接", url);
            } else {
                return R.error("未查询到数据集分享链接");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 获取数据集统计数据
     * @Date 16:46 2024/9/16
     * @Param []
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "获取数据集统计信息")
    @GetMapping("/dataset_statistic")
    public R getDatasetStatistics() {
        try {
            long metadataCount = datasetMetadataService.count();
            long tagsCount = datasetTagsService.getTagsCount();
            return R.ok("数据集数量: " + metadataCount + ", 标签种类个数: " + tagsCount);
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 查找指定数据集元数据信息
     * @Date 17:04 2024/9/16
     * @Param [jsonString]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "根据ID获取指定数据集的元数据信息")
    @PostMapping("/get_dataset_metadata_by_id")
    public R getDatasetMetadataById(@RequestBody String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String targetId = jsonObject.getString("targetId");
            DatasetResponse response = datasetMetadataService.getDatasetById(targetId);

            if (response != null) {
                return R.ok("返回指定数据集的元数据信息", response);
            } else {
                return R.error("未查询到指定数据集的元数据信息");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 查找指定数据集介绍
     * @Date 18:21 2024/9/16
     * @Param [jsonString]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "根据ID获取指定数据集的介绍信息")
    @PostMapping("/get_dataset_instruction_by_id")
    public R getDatasetInstructionById(@RequestBody String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String targetId = jsonObject.getString("targetId");

            DatasetIntroduction introduction = datasetIntroductionService.getDatasetsIntroduction(targetId);
            if (introduction != null) {
                return R.ok("返回指定数据集的介绍信息", introduction);
            } else {
                return R.error("未查询到指定数据集的介绍信息");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 查找指定数据集发布信息
     * @Date 18:27 2024/9/16
     * @Param [jsonString]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "根据ID获取指定数据集的发布信息")
    @PostMapping("/get_dataset_publish_by_id")
    public R getDatasetPublishById(@RequestBody String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String targetId = jsonObject.getString("targetId");

            DatasetPublish publish = datasetPublishService.getDatasetsPublish(targetId);
            if (publish != null) {
                return R.ok("返回指定数据集的发布信息", publish);
            } else {
                return R.error("未查询到指定数据集的发布信息");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 查找指定数据集readme文件
     * @Date 18:31 2024/9/16
     * @Param [jsonString]
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "根据ID获取指定数据集的README文件URL")
    @PostMapping("/get_dataset_Readme_by_id")
    public R getDatasetReadmeById(@RequestBody String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            String targetId = jsonObject.getString("targetId");

            String url = datasetMetadataService.getDatasetsReadme(targetId);
            if (url != null) {
                return R.ok("返回指定数据集的README文件URL", url);
            } else {
                return R.error("未查询到指定数据集的README文件URL");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error(e.getMessage());
        }
    }

    /**
     * @Author ZhangBo
     * @Description 获取最常使用的数据集标签
     * @Date 18:43 2024/9/16
     * @Param []
     * @return com.ilink.ilinkcommon.domain.R
     **/
    @MethodLog(value = "获取数据广场最常使用的8个标签")
    @GetMapping("/get_common_dataset_tags")
    public R getCommonDatasetTags() {
        try {
            int num = 8;
            List<String> commonTags = datasetTagsService.getCommonTags(num);
            if (commonTags != null) {
                return R.ok("数据广场最常使用的8个标签", commonTags);
            }
            return R.error("查询标签错误");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
}
